package com.motion.androidlearnning;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;

import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mRecyclerView = this.findViewById(R.id.content);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setAdapter(new BaseAdapter(this, Arrays.asList("View", "APT")));
    }


}

class BaseAdapter extends RecyclerView.Adapter<MyViewHolder> {
    List<String> myData;
    LayoutInflater layoutInflater;

    public BaseAdapter(Context context, List<String> data) {
        myData = data;
        layoutInflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.main_item, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.button.setText(myData.get(position));
        holder.button.setOnClickListener(view -> {
            switch (position) {
                case 0:
                    break;
                case 1:
                    ARouter.getInstance().build("/apt/main").navigation();
                    break;
            }
        });
    }

    @Override
    public int getItemCount() {
        return myData.size();

    }
}

class MyViewHolder extends RecyclerView.ViewHolder {
    public Button button;

    public MyViewHolder(View view) {
        super(view);
        button = view.findViewById(R.id.btn_title);
    }
}